Iterative Method for Recreating a Binary Tree from its Traversals
نویسندگان
چکیده
Many reconstruction algorithms for binary tree have been discussed in this paper. A particular focus of this paper is on “A new Non-Recursive Algorithm for Reconstructing a Binary Tree from its Traversals”. The computation time required for executing the reconstruction algorithm are O(N) and space complexity is O(NlogN) where N is the number of nodes in the binary tree. This algorithm works well for most of the input cases, but it has some drawbacks. There are some sequences of pre-order and in-order for which no legitimate tree can be constructed but the algorithm didn’t take these cases into consideration and constructed a wrong tree for these cases. In this paper, we have proposed a solution to the problem in the previous algorithm and designed an algorithm which is the modified version of the previous algorithm for generating a correct binary tree. The new modified algorithm is implemented in C language and tested in GCC Compiler in Linux, for all types of input cases. The New modified algorithm works well for all types of input cases. We have calculated the best case time complexity of modified algorithm and show that a correct tree can be reported in O(N) time in best case and O(NlogN) space where N is the number of nodes in the tree. We have discussed some applications of the new modified algorithm in Huffman Coding, compiler design, text processing and searching algorithms.
منابع مشابه
An improved algorithm to reconstruct a binary tree from its inorder and postorder traversals
It is well-known that, given inorder traversal along with one of the preorder or postorder traversals of a binary tree, the tree can be determined uniquely. Several algorithms have been proposed to reconstruct a binary tree from its inorder and preorder traversals. There is one study to reconstruct a binary tree from its inorder and postorder traversals, and this algorithm takes running time of...
متن کاملAn improved algorithm to reconstruct a binary tree from its inorder and postorder traversals
It is well-known that, given inorder traversal along with one of the preorder or postorder traversals of a binary tree, the tree can be determined uniquely. Several algorithms have been proposed to reconstruct a binary tree from its inorder and preorder traversals. There is one study to reconstruct a binary tree from its inorder and postorder traversals, and this algorithm takes running time of...
متن کاملA novel algorithm to determine the leaf (leaves) of a binary tree from its preorder and postorder traversals
Binary trees are essential structures in Computer Science. The leaf (leaves) of a binary tree is one of the most significant aspects of it. In this study, we prove that the order of a leaf (leaves) of a binary tree is the same in the main tree traversals; preorder, inorder, and postorder. Then, we prove that given the preorder and postorder traversals of a binary tree, the leaf (leaves) of a bi...
متن کاملConstructing a binary tree efficiently from its traversals
In this note we streamline an earlier algorithm for constructing a binary tree from its inorder and preorder traversals. The new algorithm is conceptually simpler than the earlier algorithms and its time complexity has a smaller constant factor.
متن کاملRebuilding a Tree from Its Traversals: A Case Study of Program Inversion
Given the inorder and preorder traversal of a binary tree whose labels are all distinct, one can reconstruct the tree. This article examines two existing algorithms for rebuilding the tree in a functional framework, using existing theory on function inversion. We also present a new, although complicated, algorithm by trying another possibility not explored before.
متن کامل